package com.xoa.controller.emergency;

import com.xoa.dao.emergency.CrashDispatchMapper;
import com.xoa.dao.sms.SmsMapper;
import com.xoa.model.emergency.CrashDispatch;
import com.xoa.service.emergency.CpAssetDispatchService;
import com.xoa.model.users.Users;
import com.xoa.model.emergency.CrashDispatchWithBLOBs;
import com.xoa.model.emergency.DepartmentExt;
import com.xoa.service.emergency.CrashDispatchService;
import com.xoa.util.CookiesUtil;
import com.xoa.util.LimsJson;
import com.xoa.util.common.session.SessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * @作者 杨岩林
 * @创建日期 17:022019/10/12
 * @类介绍
 */
@Controller
@RequestMapping("crash")
public class CrashDispatchController {
    @Autowired
    private CrashDispatchService crashDispatchService;

    @Autowired
    private CpAssetDispatchService cpAssetDispatchService;

    @Resource
    private CrashDispatchMapper crashDispatchMapper;

    @Resource
    private SmsMapper smsMapper;

    //应急调度页面映射
    @RequestMapping("/intRescueDispatch")
    public String intRescueDispatch(HttpServletRequest request) {

        return "app/emergencyDispatch/IntRescueDispatch";
    }
    //统计报表
    @RequestMapping("/tjechart")
    public String tjechart(HttpServletRequest request) {

        return "app/emergencyDispatch/tjechart";
    }
    //应急小分队页面映射
    @RequestMapping("/intRescueTeam")
    public String intRescueTeam(HttpServletRequest request) {
        return "app/emergencyDispatch/IntRescueTeam";
    }

    //应急事件事务提醒详情页面
    @RequestMapping("/IntRescueDetail")
    public String IntRescueDetail(HttpServletRequest request) {
        Cookie redisSessionId = CookiesUtil.getCookieByName(request, "redisSessionId");
        Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionId);
        String crasId = request.getParameter("crasId");

//        打开新增页面时消除事务提醒
        smsMapper.setReadByUrl(users.getUserId(),"/crash/IntRescueDetail?crasId="+crasId);
        return "app/emergencyDispatch/IntRescueDetail";
    }

    // 添加子任务接口
    @RequestMapping("add")
    @ResponseBody
    public LimsJson add(HttpServletRequest request, CrashDispatchWithBLOBs crashDispatchWithBLOBs){
        return crashDispatchService.insert(request,crashDispatchWithBLOBs);
    }

    // 救援人员信息确认
    @RequestMapping("confirmUsers")
    @ResponseBody
    public LimsJson confirmUsers(HttpServletRequest request,String type,Integer crasId){
        return crashDispatchService.confirmUsers(request,type,crasId);
    }

    // 删除子任务接口
    @RequestMapping("delete")
    @ResponseBody
    public LimsJson delete(Integer crasId){
        return crashDispatchService.delete(crasId);
    }


    /**
     * 查询所有子任务
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("showAll")
    @ResponseBody
    public LimsJson showAll(Integer page,Integer limit){
        return crashDispatchService.queryAll(page,limit);
    }

    // 查看详情接口 type 1是人员详情查询 2是物资详情查询
    @RequestMapping("showDetails")
    @ResponseBody
    public LimsJson showDetails(Integer crasId,String type){
        return crashDispatchService.showDetails(crasId,type);

    }

    /**
     * 修改子任务
     * @param crashDispatchWithBLOBs
     * @return
     */
    @RequestMapping("change")
    @ResponseBody
    public LimsJson change(HttpServletRequest request ,CrashDispatchWithBLOBs crashDispatchWithBLOBs){

        Cookie redisSessionId = CookiesUtil.getCookieByName(request, "redisSessionId");
        Users users =  SessionUtils.getSessionInfo(request.getSession(),Users.class,new Users(),redisSessionId);

        LimsJson toJson = crashDispatchService.update(request,crashDispatchWithBLOBs);
        if(toJson.getCode() == 0){
            cpAssetDispatchService.addsDispatch(crashDispatchWithBLOBs,users.getUserId());
        }
        return toJson;
    }

    /**
     * 查找最后一个子任务所选的人员信息
     * @return
     */
    @RequestMapping("getLastDisp")
    @ResponseBody
    public LimsJson getLastDisp(HttpServletRequest request ){
        LimsJson toJson = new LimsJson(0,"err");
        CrashDispatch crashDispatch = crashDispatchMapper.selectLast();
        if(crashDispatch!=null ){
            toJson.setCode(0);
            toJson.setMsg("true");
            toJson.setObj(crashDispatch);
        }
        return toJson;
    }


    /**
     * 查询应急救援设备类型
     * @return
     */
    @RequestMapping("showAssetsType")
    @ResponseBody
    public LimsJson showAssetsType(){
        return crashDispatchService.queryType();
    }

    /**
     * 查询应急救援设备
     * @param typeId
     * @param page
     * @param limit
     * @param assetName
     * @param info
     * @param type(应急设备-1)
     * @return
     */
    @RequestMapping("showAssets")
    @ResponseBody
    public LimsJson showAssets(Integer typeId,Integer page,Integer limit,String assetName,String info, String type){
        return crashDispatchService.queryByType(typeId,page,limit,assetName,info,type);
    }

    /**
     * 查询救援人员类型
     * @return
     */
    @RequestMapping("showUserType")
    @ResponseBody
    public LimsJson showUserType(){
        return crashDispatchService.queryUserType();
    }

    /**
     * 查询应急救援人员
     * @param typeName
     * @param page
     * @param limit
     * @param staffName
     * @param deptId
     * @param type(应急人员-1)
     * @return
     */
    @RequestMapping("showUser")
    @ResponseBody
    public LimsJson showUser(String typeName,Integer page,Integer limit,String staffName,Integer deptId, String type){
        return crashDispatchService.queryUserByType(typeName,page,limit,staffName,deptId,type);
    }

    /**
     * 导出PDF
     * @param htmlContent
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("toPDF")
    public LimsJson toPDF(String htmlContent, HttpServletRequest request){
        return crashDispatchService.toPDF(htmlContent, request);
    }

    /**
     * 查询子部门
     * @param deptId
     * @return
     */
    @ResponseBody
    @RequestMapping("showDepts")
    public LimsJson<DepartmentExt> showDepts(Integer deptId){
        return crashDispatchService.showDepts(deptId);
    }


}
